'use strict';
exports = module.exports = function ($scope, $uibModalInstance, $API, result, bwtype) {
    /*数据源与数据集*/
    $scope.data = [];
    $scope.result = result;
    if(bwtype == null){
        bwtype=0;
    }
    $scope.bwtype = bwtype;
    /*本页使用分页数*/
    var page = 0,
        pageCount = 20,
        searchValue = "",
        totalPage = 0;
    /*可搜索项*/
    $scope.searchValue = "";
    $scope.readmoreVisible = true;
    /*搜索*/
    $scope.searchData = function(){
        if( $scope.searchValue == searchValue ) return;
        searchValue = $scope.searchValue
        page = 0;
        $scope.loadMoreData(1);
    };
    /*ajax加载更多*/
    $scope.loadMoreData = function (flag) {
        page += 1;
        if( page && totalPage && totalPage < page ) return;
        $API.Drop.Manager('', {
            searchValue:searchValue, // 搜索用户名或者真实姓名 ,模糊查询
            page:page, //第几页
            pagesize:pageCount // 每页条数
        }).then(function(res){
            var nowDatas = checkResult( res.data , flag );
            $scope.data = nowDatas;
            totalPage = Math.ceil( parseInt( res.data.rowcount ) / pageCount );
            $scope.readmoreVisible = (totalPage-1 >= page);
        });
    }
    $scope.loadMoreData();
    /*检查数据源是否被选中*/
    function checkResult ( datas , flag ) {
        var ids = ",",
            oldDatas = (page == 1 || flag == 1) ? [] : angular.copy($scope.data);
        for( var i=0,l=$scope.result.length;i<l;i++){
            ids += $scope.result[i]["id"]+",";
        }
        for( var i=0,l=datas.length;i<l;i++){
            datas[i]["check"] = ( ids.indexOf( ","+datas[i]["id"]+"," ) > -1 );
            oldDatas.push(datas[i]);
        }
        return oldDatas;
    }
    /*修改选中状态*/
    $scope.changeDataFun = function ( row ) {
        var index = isInResult(row);
        if( row.check && index == -1 ){
            $scope.result.push( angular.copy(row) );
        }else if( ! row.check && index > -1 ){
            $scope.result.splice(index,1);
        }
    };
    /*清除某一个结果*/
    $scope.delResultFun = function ( row ) {
        var index1 = isInResult(row),
            index2 = isInData(row);
        $scope.result.splice(index1,1);
        if(index2==-1){return}
        $scope.data[index2].check = false;
    };
    function isInResult(row){
        var l = $scope.result.length;
        for( var i=0;i<l;i++){ if( $scope.result[i].id == row.id ){ return i; } }
        return -1;
    }
    function isInData(row){
        var l = $scope.data.length;
        for( var i=0;i<l;i++){ if( $scope.data[i].id == row.id ){ return i; } }
        return -1;
    }
    /*清空结果*/
    $scope.clearResult = function () {
        var l = $scope.data.length;
        for( var i=0;i<l;i++){ $scope.data[i].check =false; }
        $scope.result.length = 0;
    };
    $scope.ok = function () {
        $scope.adminResult=angular.copy($scope.result);
        $uibModalInstance.close({
            result : $scope.adminResult,
            count : $scope.result.length
        });
        $scope.cancel();
    };
    $scope.cancel = function () {
        $uibModalInstance.dismiss('cancel');
    };
};
